package com.lagou.homework141;

import java.io.*;
import java.util.LinkedList;
import java.util.List;


public class StudentInfoManager {

    private static List<Student> studentList = new LinkedList<>();

    private static final String path = "E:\\141.txt";

    public static void add(Student stu) {
        studentList.add(stu);
        System.out.println("添加成功！");
//        findAll();
    }

    public static Student find(String name) {
        for (Student stu : studentList) {
            if (name.equals(stu.getName()))
                return stu;
        }
        System.out.println("不存在" + name + "这个学生");
        return null;
    }

    public static void findAll() {
        System.out.println("所有数据：");
        for (int i = 0; i < studentList.size(); i++)
            System.out.println(i + " : " + studentList.get(i));
        System.out.println("----------------------------------------");
    }

    public static void update(int index, String name) {
        Student stu = studentList.get(index);
        System.out.println("修改前：" + stu);
        stu.setName(name);
        studentList.set(index, stu);
        System.out.println("修改后：" + studentList.get(index));
//        initUI();
    }

    public static void delete(String name) {
        Student stu = find(name);
        if (stu != null)
            studentList.remove(stu);
        findAll();
    }

    public static void createFile() {
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
                System.out.println("创建文件...");
                writeFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void readFile(boolean isStart) {
        createFile();
        ObjectInputStream objectInputStream = null;
        Object obj = null;
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream(path));
            obj = objectInputStream.readObject();
            if (obj != null) {
                System.out.println("文件读取结果：" + obj);
                if (isStart)
                    studentList = (List<Student>) obj;
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (objectInputStream != null
            ) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void writeFile() {
//        createFile();
        //当希望将多个对象写入文件时，通常建议将多个对象放入一个集合中，然后将集合这个整体看做一
        //个对象写入输出流中，此时只需要调用一次readObject方法就可以将整个集合的数据读取出来，
        //从而避免了通过返回值进行是否达到文件末尾的判断。
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(new FileOutputStream(path));
            objectOutputStream.writeObject(studentList);
            System.out.println("写入数据：" + studentList);
            objectOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
